﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.IO;
using System;


namespace Nirvana
{

	[CustomEditor(typeof(SceneLogic))]
	public class SceneLogicEditor : Editor
	{
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            var logic = this.target as SceneLogic;
            logic.gameObject.tag = "EditorOnly";

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Create NPC"))
            {
                this.CreateNPC(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            if (GUILayout.Button("Create Monster"))
            {
                this.CreateMonster(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            if (GUILayout.Button("Create DovePoint"))
            {
                this.CreateDovePoint(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Create Door"))
            {
                this.CreateDoor(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            if (GUILayout.Button("Create Gather"))
            {
                this.CreateGather(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Create Fence"))
            {
                this.CreateFence(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            if (GUILayout.Button("Create jumppoint"))
            {
                this.CreateJumppoint(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            GUILayout.EndHorizontal();

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("Create Effect"))
            {
                this.CreateEffect(logic);
                EditorSceneManager.MarkSceneDirty(logic.gameObject.scene);
            }

            GUILayout.EndHorizontal();

            EditorGUILayout.Space();
            if (GUILayout.Button("Save Scene Lua"))
            {
                this.SaveSceneLua(logic);
            }

            if (GUILayout.Button("Save Scene XML"))
            {
                this.SaveSceneXML(logic);
            }

            if (GUILayout.Button("Save Map XML"))
            {
                this.SaveMapXML(logic);
            }
            if (GUILayout.Button("Save Scene Robot"))
            {
                this.SaveSceneRobotLua(logic);
            }
      
        }
        private void CreateNPC(SceneLogic logic)
        {
            var go = new GameObject("NPC", typeof(SceneNPC));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Blue);
            this.PlaceObject(go);
        }

        private void CreateEffect(SceneLogic logic)
        {
            var go = new GameObject("Effect", typeof(SceneEffect));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Purple);
            this.PlaceObject(go);
        }

        private void CreateMonster(SceneLogic logic)
        {
            var go = new GameObject("Monster", typeof(SceneMonster));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Red);
            this.PlaceObject(go);
        }

        private void CreateDovePoint(SceneLogic logic)
        {
            var go = new GameObject("DovePoint", typeof(SceneDovePoint));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Purple);
            this.PlaceObject(go);
        }

        private void CreateDoor(SceneLogic logic)
        {
            var go = new GameObject("Door", typeof(SceneDoor));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Orange);
            this.PlaceObject(go);
        }

        private void CreateGather(SceneLogic logic)
        {
            var go = new GameObject("Gather", typeof(SceneGatherPoint));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Gray);
            this.PlaceObject(go);
        }

        private void CreateFence(SceneLogic logic)
        {
            var go = new GameObject("Fence", typeof(SceneFence));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Teal);
            this.PlaceObject(go);
        }

        private void CreateJumppoint(SceneLogic logic)
        {
            var go = new GameObject("Jumppoint", typeof(SceneJumppoint));
            go.transform.SetParent(logic.transform, false);
            go.SendMessage("OnValidate");
            SceneTagManager.SetIcon(go, SceneTagManager.LabelIcon.Teal);
            this.PlaceObject(go);
        }

        private void PlaceObject(GameObject go)
        {
            if (SceneView.lastActiveSceneView == null)
                return;

            var camera = SceneView.lastActiveSceneView.camera;
            var ray = new Ray(camera.transform.position, camera.transform.forward);
            var hits = Physics.RaycastAll(ray, Mathf.Infinity);
            if (hits.Length > 0)
            {
                go.transform.position = hits[0].point;
            }
        }

        private void SaveSceneLua(SceneLogic logic)
        {
            var defaultPath = Application.dataPath + "/Game/Lua/config/scenes/";
            defaultPath = Path.GetFullPath(defaultPath);
            if (!Directory.Exists(defaultPath))
            {
                Directory.CreateDirectory(defaultPath);
            }

            var filePath = EditorUtility.SaveFilePanel(
                "Save File...",
                defaultPath,
                "scene_" + logic.SceneID.ToString(),
                "lua");
            if (!string.IsNullOrEmpty(filePath))
            {
                if (!logic.SaveSceneLua(filePath, true))
                {
                    EditorUtility.DisplayDialog("非法操作", "请检查场景", "确定");
                }
            }
        }

        private void SaveSceneXML(SceneLogic logic)
        {
            var defaultPath = Application.dataPath + "/../Config/Scene/";
            defaultPath = Path.GetFullPath(defaultPath);
            if (!Directory.Exists(defaultPath))
            {
                Directory.CreateDirectory(defaultPath);
            }

            var filePath = EditorUtility.SaveFilePanel(
                "Save File...",
                defaultPath,
                logic.SceneID.ToString(),
                "xml");
            if (!string.IsNullOrEmpty(filePath))
            {
                logic.SaveSceneXML(filePath);
            }
        }

        private void SaveMapXML(SceneLogic logic)
        {
            var defaultPath = Application.dataPath + "/../Config/Map/";
            defaultPath = Path.GetFullPath(defaultPath);
            if (!Directory.Exists(defaultPath))
            {
                Directory.CreateDirectory(defaultPath);
            }

            var filePath = EditorUtility.SaveFilePanel(
                "Save File...",
                defaultPath,
                logic.SceneID.ToString(),
                "xml");
            if (!string.IsNullOrEmpty(filePath))
            {
                logic.SaveMapXML(filePath);
            }
        }

        private void SaveSceneRobotLua(SceneLogic logic)
        {
            var defaultPath = Application.dataPath + "/../Robot/";
            defaultPath = Path.GetFullPath(defaultPath);
            if (!Directory.Exists(defaultPath))
            {
                Directory.CreateDirectory(defaultPath);
            }

            var filePath = EditorUtility.SaveFilePanel(
                "Save File...",
                defaultPath,
                "scene_" + logic.SceneID.ToString(),
                "lua");
            if (!string.IsNullOrEmpty(filePath))
            {
                logic.SaveSceneLua(filePath, false);
            }
        }
    }
}

